<template>
  <div class="Collection">
    <!-- 顶部标题 -->
    <top-title-bar>
      <template #left>
        <div class="left" @click="$router.go(-1)">
          <span class="iconfont icon-xiangzuo1"></span>
          <div class="title">
            {{ $route.query.user_id == 20096544 ? "我的收藏" : "Ta的收藏" }}
          </div>
        </div>
      </template>
    </top-title-bar>

    <div class="wrap">
      <div class="post-mag-box" v-if="CollectionData">
        <div class="body">
          <router-link
            tag="div"
            :to="'/PostDetails?postID=' + post.postID + '&from=技术分享'"
            class="post-item-box"
            v-for="post in CollectionData.posts"
            :key="post.postID"
          >
            <!-- 没有图片 -->
            <div class="post-item none-pic" v-if="post.images.length == 0">
              <div class="text">
                <div
                  class="title text-ellipsis-2"
                  v-html="HtmlToText(post.title)"
                ></div>

                <div class="content">
                  <span
                    class="text-ellipsis-2"
                    v-html="HtmlToText(post.detail)"
                  >
                  </span>
                </div>

                <div class="info">
                  <div class="left">
                    <div class="name">
                      <small>{{ post.user.nick }}</small>
                    </div>

                    <div class="time">
                      <small>{{ post.createTime | numToDate }}</small>
                    </div>
                  </div>

                  <div class="right">
                    <div class="seek">
                      <i></i
                      ><small class="iconfont icon-kanguo_S">{{
                        post.hit
                      }}</small>
                    </div>

                    <div class="msg">
                      <i></i
                      ><small class="iconfont icon-xiaoxi1">{{
                        post.commentCount
                      }}</small>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <!-- 只有一张图 -->
            <div class="post-item one-pic" v-if="post.images.length == 1">
              <div class="icon"><img :src="post.images[0]" alt="" /></div>

              <div class="text">
                <div class="title text-ellipsis-1" v-html="post.title"></div>

                <div class="content">
                  <span class="text-ellipsis-3" v-html="post.detail"> </span>
                </div>

                <div class="info">
                  <div class="left">
                    <div class="name">
                      <small>{{ post.user.nick }}</small>
                    </div>

                    <div class="time">
                      <small>{{ post.createTime | numToDate }}</small>
                    </div>
                  </div>

                  <div class="right">
                    <div class="seek">
                      <i></i
                      ><small class="iconfont icon-kanguo_S">{{
                        post.hit
                      }}</small>
                    </div>

                    <div class="msg">
                      <i></i
                      ><small class="iconfont icon-xiaoxi1">{{
                        post.commentCount
                      }}</small>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <!-- 两张图以上 -->
            <div class="post-item many-pic" v-if="post.images.length > 1">
              <div class="text">
                <div class="title text-ellipsis-1" v-html="post.title"></div>

                <div class="content">
                  <span
                    class="text-ellipsis-3"
                    v-html="TextToTitle(HtmlToText(post.detail))"
                  >
                  </span>
                </div>

                <div class="imgs" v-if="post.images.length < 3">
                  <div class="img" v-for="img in post.images.length" :key="img">
                    <img :src="post.images[img - 1]" alt="" />
                  </div>
                </div>
                <div class="imgs" v-if="post.images.length >= 3">
                  <div class="img" v-for="img in 3" :key="img">
                    <img :src="post.images[img - 1]" alt="" />
                  </div>
                  <b>{{ post.images.length }}图</b>
                </div>

                <div class="info">
                  <div class="left">
                    <div class="name">
                      <small>{{ post.user.nick }}</small>
                    </div>

                    <div class="time">
                      <small>{{ post.createTime | numToDate }}</small>
                    </div>
                  </div>

                  <div class="right">
                    <div class="seek">
                      <i></i
                      ><small class="iconfont icon-kanguo_S">{{
                        post.hit
                      }}</small>
                    </div>

                    <div class="msg">
                      <i></i
                      ><small class="iconfont icon-xiaoxi1">{{
                        post.commentCount
                      }}</small>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </router-link>
          <van-empty
            v-show="!CollectionData.posts.length"
            class="custom-image"
            image="./img/empty.png"
            description="去其他地方看看吧!"
          />
        </div>
      </div>
    </div>
  </div>
</template>



<script>
import TopTitleBar from "../components/TopTitleBar.vue";
import { getCollectionViewData } from "../assets/api/space.js";
import { getPostJson } from "../assets/api/posts.js";
export default {
  data() {
    return { CollectionData: null };
  },
  methods: {
    // 数据初始化
    async dataInit() {
      await this.CollectionDataInit();
      await this.localCollectionDataInit();
    },

    // 本地数据
    async localCollectionDataInit() {
      if (this.$route.query.user_id != 20096544) return;
      let data = JSON.parse(localStorage.getItem("PostFavoriteList"));
      if (!data) return console.log("本地无更新");
      else {
        let user_id = this.$route.query.user_id;
        if (user_id != 20096544)
          return console.log("不是登录用户 无需检查本地更新");
        else {
          // 过滤出收藏的
          data = data.filter((item) => item.isFavorite == 1);
          // 请求本地json
          let postListData = await getPostJson();
          //偷懒写法 当前就一个 就直接取 postCategoryListSortBy1 和 index
          let index = 0;
          // 拿到列表
          let posts = postListData.postCategoryListSortBy1[index].posts;
          data.forEach((item) => {
            let temp = posts.find((obj) => obj.postID == item.post_id);
            if (temp) this.CollectionData.posts.unshift(temp);
          });
          console.log(
            "有更新 这是合并后的数据 CollectionData =>",
            this.CollectionData
          );
        }
      }
    },

    // 收藏数据初始化
    async CollectionDataInit() {
      let user_id;
      if ("user_id" in this.$route.query) user_id = this.$route.query.user_id;
      else user_id = 20096544;
      let start = 0;
      let groupId = 0;
      let count = 20;
      let data = await getCollectionViewData({
        user_id,
        start,
        groupId,
        count,
      });
      this.CollectionData = data;
      console.log("这是取到的数据 CollectionData =>", this.CollectionData);
    },

    // html代码转成字符
    HtmlToText(html) {
      let text = html;
      text = text.split("<text>").join("");
      text = text.split("</text>").join("");
      text = text.split("<image>").join("");
      text = text.split("</image>").join("");
      return text;
    },

    // 将很长的字符只取第一句
    TextToTitle(text) {
      let title = text;
      let arr = [];
      if (text.indexOf(" ") != -1) arr.push(text.indexOf(" "));
      if (text.indexOf("，") != -1) arr.push(text.indexOf("，"));
      if (text.indexOf(".") != -1) arr.push(text.indexOf("."));
      if (text.indexOf("。") != -1) arr.push(text.indexOf("。"));
      if (text.indexOf("；") != -1) arr.push(text.indexOf("；"));
      if (text.indexOf(";") != -1) arr.push(text.indexOf(";"));

      if (!arr.length) return title;

      arr = arr.sort((a, b) => a - b);
      return text.substring(0, arr[0]);
    },
  },
  created() {
    this.dataInit();
  },
  components: {
    TopTitleBar,
  },
  filters: {
    numToDate(val) {
      if (val > 1000000000) {
        let oldTime = parseInt(val);
        let newTime = new Date().getTime();
        let dTime = newTime - oldTime;
        if (dTime / 1000 < 60) return parseInt(dTime / 1000) + "秒前";
        else if (dTime / 1000 / 60 < 60)
          return parseInt(dTime / 1000 / 60) + "分前";
        else if (dTime / 1000 / 60 / 60 < 24)
          return parseInt(dTime / 1000 / 60 / 60) + "小时前";
        else if (dTime / 1000 / 60 / 60 / 24 < 30)
          return parseInt(dTime / 1000 / 60 / 60 / 24) + "天前";
        else if (dTime / 1000 / 60 / 60 / 24 / 30 < 12)
          return parseInt(dTime / 1000 / 60 / 60 / 24 / 30) + "月前";
        else return parseInt(dTime / 1000 / 60 / 60 / 24 / 30 / 12) + "年前";
      }
    },
  },
};
</script>



<style lang="scss" scoped>
.Collection {
  min-height: 100%;
  background-color: #fff;
  .TopTitleBar {
    .left {
      display: flex;
      align-items: center;
      .icon-xiangzuo1 {
        font-weight: bold;
        color: #fff;
        font-size: 22px;
      }
    }
    .right {
      .icon-xiaoxi,
      .icon-bianji1 {
        font-weight: bold;
        color: #fff;
        font-size: 22px;
        &.icon-xiaoxi {
          margin-left: 18px;
        }
      }
    }
  }

  .wrap {
    padding-top: 0;
    .post-mag-box {
      background-color: #fff;

      .head {
        display: flex;
        height: 42px;
        line-height: 42px;
        overflow-x: scroll;
        font-size: 15px;
        color: #646464;

        p {
          height: 42px;
          box-sizing: border-box;
          border-bottom: 1px solid #e0e0e0;
          white-space: nowrap;
          padding: 0 12px;
        }

        .con {
          color: #14ca61;
          position: relative;

          &::after {
            content: "";
            display: block;
            position: absolute;
            height: 2px;
            left: 0;
            right: 0;
            bottom: -1px;
            width: calc(100% - 24px);
            margin: auto;
            background-color: #0cc85c;
          }
        }

        &::-webkit-scrollbar {
          width: 0;
          height: 0;
        }

        &::-webkit-scrollbar-thumb {
          width: 0;
          height: 0;
        }
      }

      .body {
        padding: 0 12px;
      }
    }

    // -----
    .post-item {
      padding: 13px 0;
      display: flex;
      border-bottom: 1px solid #dddddd;

      .icon {
        width: 91px;
        height: 91px;
        border-radius: 5px;
        overflow: hidden;
      }

      .text {
        flex: 1;
        margin-left: 8px;
      }

      .title {
        line-height: 21px;
        color: #0e0e0e;
        font-size: 15px;
        margin-bottom: 5px;
      }

      .content {
        height: 42px;
        line-height: 14px;
        color: #646464;
        font-size: 12px;
        margin-bottom: 11px;
      }

      .info {
        display: flex;
        justify-content: space-between;

        .name {
          small {
            color: #969696;
          }
        }

        .time {
          small {
            color: #cccccc;
          }
        }
      }

      .seek {
        i {
          content: "";
          display: inline-block;
          background-size: 69px 13px;
          width: 16px;
          height: 11px;
          position: relative;
          vertical-align: middle;
        }
      }

      .msg {
        margin-left: 5px;

        i {
          content: "";
          display: inline-block;
          background-size: 69px 13px;
          width: 13px;
          height: 13px;
          position: relative;
          vertical-align: middle;
        }
      }
    }
    .post-mag-box {
      ::v-deep .van-sticky {
        border-bottom: 1px solid #dedede;
        .van-tabs__nav {
          padding-left: 12px;
          padding-right: 12px;
          .van-tab {
            padding: 0;
            margin-right: 25px;
            &:nth-last-child(2) {
              margin-right: 0;
            }
          }
        }
      }

      .body {
        padding: 0;
        .post-item-box {
          padding: 0 12px;

          &:active {
            opacity: 0.9;
            box-shadow: 0 0 100px 0 inset rgba($color: #000000, $alpha: 0.2);
          }

          &:last-child {
            .post-item {
              border-bottom-width: 0;
            }
          }

          .post-item {
            padding: 0 0 10px;
            border-bottom: 1px solid #dedede;

            .text {
              width: 100%;
              .title {
                background-color: transparent;
              }
              .content {
                height: 48px;
                line-height: 16px;
              }
              .info {
                .right {
                  .iconfont {
                    font-size: 12px;
                    &::before {
                      margin-right: 3px;
                    }
                  }
                }
              }
            }

            // 只有一张图
            &.one-pic {
              padding-top: 8px;
              .icon {
                flex-shrink: 0;
              }
              .text {
                width: calc(100% - 99px);
              }
              .content {
                color: #646464;
                margin-bottom: 7px;

                .title {
                  padding-left: 0;
                  padding-right: 0;
                }
              }
            }

            // 一张图都没
            &.none-pic {
              .text {
                margin-left: 0;
                width: 100%;
                .title {
                  max-height: 42px;
                  padding: 0;
                  padding-top: 8px;
                }
                .content {
                  max-height: 28px;
                  height: auto;
                }
              }
            }

            // 两张图以上
            &.many-pic {
              .text {
                padding-top: 8px;
                margin-left: 0;

                .content {
                  height: auto;
                  max-height: 48px;
                }

                .imgs {
                  height: 110px;
                  border-radius: 5px;
                  display: flex;
                  overflow: hidden;
                  position: relative;
                  margin-bottom: 8px;
                  .img {
                    width: 110px;
                    height: 110px;
                    overflow: hidden;
                    border-radius: 5px;
                    margin-right: calc((100% - 330px) / 2);
                    &:nth-child(3) {
                      margin-right: 0;
                    }
                  }
                  b {
                    position: absolute;
                    right: 0;
                    bottom: 0;
                    font-size: 12px;
                    color: #fff;
                    width: max-content;
                    height: 18px;
                    line-height: 18px;
                    display: block;
                    padding: 0 4px;
                    background-color: rgba($color: #000000, $alpha: 0.2);
                    border-radius: 5px 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    .post-item .info .left,
    .post-item .info .right {
      display: flex;
      font-size: 12px;
      height: 12px;
      line-height: 12px;
      .time {
        margin-left: 3px;
      }
    }
  }
}
</style>